﻿using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Jungle.Code.DAL;
using Jungle.Code.Utils;

public partial class news : System.Web.UI.UserControl
{
    private int LangId = 1;
    protected override void Render(HtmlTextWriter writer)
    {
        HttpCookie cookie = Request.Cookies["CurrLang"];
        if(cookie != null) LangId = Convert.ToInt32(cookie["lang"]);

        writer.AddAttribute(HtmlTextWriterAttribute.Width, "590px");
        writer.AddAttribute(HtmlTextWriterAttribute.Bgcolor, "#FFFFFF");
        writer.AddStyleAttribute(HtmlTextWriterStyle.BorderWidth, "15");
        writer.AddStyleAttribute(HtmlTextWriterStyle.BorderStyle, "solid");
        writer.AddStyleAttribute(HtmlTextWriterStyle.BorderColor, "#FFFFFF");
        writer.RenderBeginTag(HtmlTextWriterTag.Table);
        writer.RenderBeginTag(HtmlTextWriterTag.Tr);
        writer.RenderBeginTag(HtmlTextWriterTag.Td);
        writer.AddAttribute(HtmlTextWriterAttribute.Href, "Modules.aspx?mod=Articles&cmd=addNews");
        writer.RenderBeginTag(HtmlTextWriterTag.A);
        writer.AddAttribute(HtmlTextWriterAttribute.Src, "../Modules/Articles/img/addCategory.jpg");
        writer.AddAttribute(HtmlTextWriterAttribute.Alt, "Добавить статью");
        writer.RenderBeginTag(HtmlTextWriterTag.Img);
        writer.RenderEndTag();
        writer.RenderEndTag();
        if(Request["cmd"].ToString() == "nbc")
            writer.Write("&nbsp;<strong>Новости этой категории:</strong>");
        else
        writer.Write("&nbsp;<strong>Полный список статей:</strong>");
        writer.Write("<br>");

        if (Request["cmd"].ToString() == "nbc")
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand command = new SqlCommand("articles.mdNews_GetByCategory", connect);
                SqlParameter param_id_category = command.Parameters.Add("@id_category", SqlDbType.Int);
                SqlDataReader reader = null;
                command.CommandType = CommandType.StoredProcedure;
                param_id_category.Value = Convert.ToInt32(Request["id_category"]);

                command.Connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    writer.Write("&nbsp;&nbsp;&nbsp;");
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, "Modules.aspx?mod=Articles&cmd=updateNews&id_news=" + reader.GetValue(0).ToString());
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, "../Modules/News/img/rss.jpg");
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);
                    writer.RenderEndTag();
                    writer.Write("&nbsp;" + reader.GetValue(1));
                    writer.RenderEndTag();

                    writer.Write("&nbsp;");
                    writer.AddAttribute(HtmlTextWriterAttribute.Href, "Modules.aspx?mod=Articles&cmd=nbc&id_category=" + Request["id_category"] + "&cmd2=deleteN&id_news=" + reader.GetValue(0).ToString());
                    writer.RenderBeginTag(HtmlTextWriterTag.A);
                    writer.AddAttribute(HtmlTextWriterAttribute.Src, "../Modules/News/img/del2.jpg");
                    writer.AddAttribute(HtmlTextWriterAttribute.Alt, "Удалить");
                    writer.RenderBeginTag(HtmlTextWriterTag.Img);
                    writer.RenderEndTag();
                    writer.RenderEndTag();
                    writer.Write("<br>");

                }
                reader.Close();
                command.Connection.Close();
            }
        else
            using (SqlConnection connect = new SqlConnection(DBHelper.ConnectionString))
            {
                SqlCommand command = new SqlCommand("Articles.mdNews_GetAll", connect);
                SqlParameter param_type = command.Parameters.Add("@type", SqlDbType.NChar, 20);
                SqlDataReader reader = null;
                command.CommandType = CommandType.StoredProcedure;
                param_type.Value = "GetNews";

                command.Connection.Open();
                reader = command.ExecuteReader();
                while (reader.Read())
                {
                    if (LangId == Convert.ToInt32(reader["languageId"]))
                    {
                        writer.Write("&nbsp;&nbsp;&nbsp;");
                        writer.AddAttribute(HtmlTextWriterAttribute.Href, "Modules.aspx?mod=Articles&cmd=updateNews&id_news=" + reader.GetValue(0).ToString());
                        writer.RenderBeginTag(HtmlTextWriterTag.A);
                        writer.AddAttribute(HtmlTextWriterAttribute.Src, "../Modules/Articles/img/rss.jpg");
                        writer.RenderBeginTag(HtmlTextWriterTag.Img);
                        writer.RenderEndTag();
                        writer.Write("&nbsp;" + reader.GetValue(1));
                        writer.RenderEndTag();

                        writer.Write("&nbsp;");
                        writer.AddAttribute(HtmlTextWriterAttribute.Href, "Modules.aspx?mod=Articles&cmd=News&id_category=" + Request["id_category"] + "&cmd2=deleteN&id_news=" + reader.GetValue(0).ToString());
                        writer.RenderBeginTag(HtmlTextWriterTag.A);
                        writer.AddAttribute(HtmlTextWriterAttribute.Src, "../Modules/Articles/img/del2.jpg");
                        writer.AddAttribute(HtmlTextWriterAttribute.Alt, "Удалить");
                        writer.RenderBeginTag(HtmlTextWriterTag.Img);
                        writer.RenderEndTag();
                        writer.RenderEndTag();


                        writer.Write("<br>");
                    }

                }
                reader.Close();
                command.Connection.Close();
            }

        writer.RenderEndTag();
        writer.RenderEndTag();
            writer.RenderEndTag();
      
        base.Render(writer);
    }

}
